//
//  HHSendCodeTableListCell.m
//  Smart
//
//  Created by Hayder on 2022/12/27.
//

#import "HHSendCodeTableListCell.h"

@implementation HHSendCodeTableListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.blankView addSubview:self.textfield];
        [self.blankView addSubview:self.sendCodeBtn];
    }
    return self;
}

- (void)clickButtonEvent
{
    if(self.item.didClickSendCellBlock)
    {
        self.item.didClickSendCellBlock(self.item,self.sendCodeBtn);
    }
}


- (void)setItem:(HHSendCodeListItem *)item
{
    _item = item;
    [self setConfigWithItem:item];
    
    self.textfield.placeholder = self.item.placeholder?self.item.placeholder:@"请输入验证码";
    
    [self setNeedsLayout];
}

- (void)textFieldTextChanged:(UITextField *)textField
{
    self.item.contentText = textField.text;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat contentW = self.blankView.width;
    CGFloat contentH = self.blankView.height;
    
    self.sendCodeBtn.frame = CGRectMake(contentW - 80 - self.margin, 0, 80, contentH);
    
    self.textfield.frame = CGRectMake(self.margin, 0, contentW - 2*self.margin - self.sendCodeBtn.width, contentH);
}

- (UITextField *)textfield
{
    if(!_textfield)
    {
        _textfield = [[UITextField alloc] init];
        _textfield.font = Font(15);
        _textfield.textColor = kGray_51;
        _textfield.keyboardType = UIKeyboardTypeNumberPad;
        [_textfield addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textfield;
}

- (UIButton *)sendCodeBtn
{
    if(!_sendCodeBtn)
    {
        _sendCodeBtn = [UIButton buttonWithUIProperty:HHButtonProperty.init()
                        .fontSize(14)
                        .titleColor(kGray_150)
                        .title(@"获取验证码")
        .targetAction(self,@selector(clickButtonEvent))];
    }
    
    return _sendCodeBtn;
}

@end
